package cn.stylefeng.guns.modular.reimbursement.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.reimbursement.entity.Reimbursement;
import cn.stylefeng.guns.modular.reimbursement.mapper.ReimbursementMapper;
import cn.stylefeng.guns.modular.reimbursement.model.params.ReimbursementParam;
import cn.stylefeng.guns.modular.reimbursement.model.result.ReimbursementResult;
import cn.stylefeng.guns.modular.reimbursement.service.ReimbursementService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 报销表 服务实现类
 * </p>
 *
 * @author
 * @since 2019-10-14
 */
@Service
public class ReimbursementServiceImpl extends ServiceImpl<ReimbursementMapper, Reimbursement> implements ReimbursementService {

    @Override
    public void add(ReimbursementParam param) {
        Reimbursement entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ReimbursementParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ReimbursementParam param) {
        Reimbursement oldEntity = getOldEntity(param);
        Reimbursement newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ReimbursementResult findBySpec(ReimbursementParam param) {
        return null;
    }

    @Override
    public List<ReimbursementResult> findListBySpec(ReimbursementParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ReimbursementParam param) {
        //Page pageContext = getPageContext();
        param.setUserId(ShiroKit.getUser().getId());
        List<ReimbursementResult> list = this.baseMapper.customList(param);
        LayuiPageInfo result = new LayuiPageInfo();
        result.setData(list);
        return result;
    }

    @Override
    public Integer findMoneyByProject(Integer projectId, String type) {
        Integer moneyByProject = this.baseMapper.findMoneyByProject(projectId, type);
        return moneyByProject == null ? 0 : moneyByProject;
    }

    private Serializable getKey(ReimbursementParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Reimbursement getOldEntity(ReimbursementParam param) {
        return this.getById(getKey(param));
    }

    private Reimbursement getEntity(ReimbursementParam param) {
        Reimbursement entity = new Reimbursement();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
